package org.irri.smta.rest.impl;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.codehaus.jackson.map.ObjectMapper;
import org.irri.smta.common.Constants;
import org.irri.smta.domain.ReportLog;
import org.irri.smta.domain.SmtaReport;
import org.irri.smta.rest.IRestfulService;
import org.irri.smta.service.IReportLogService;
import org.irri.smta.service.ISmtaGenerateService;
import org.irri.smta.ui.bean.ResourceConfigBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SmtaGenerateRestfulService implements IRestfulService {
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(SmtaGenerateRestfulService.class);
	
	@Inject
	@Named("resourceConfigBean")
	private ResourceConfigBean resourceConfigBean;

	@Inject
	@Named("smtaGenerateService")
	private ISmtaGenerateService<SmtaReport, Long> smtaGenerateService;
	
	@Inject
	@Named("reportLogService")
	private IReportLogService<ReportLog, Long> reportLogService;

	@RequestMapping(value = "/smtagenerate/symbol/{symbol}/shuRequestId/{shuRequestId}", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public String getGenerateSmtaAsJson(@PathVariable(value = "symbol") String symbol, @PathVariable(value = "shuRequestId") String shuRequestId) {
		LOGGER.info("Generating PDF for SMTA ID = " + symbol + " and SHU REQUEST ID = " + shuRequestId);
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> items = new HashMap<String, Object>();
		
		Map<String, String> paramMap = new HashMap<String, String>();
		String smtaTemplateDirectory = resourceConfigBean.getSmtaTemplateDirectory();
		String smtaTemplateFilename = resourceConfigBean.getSmtaTemplateFilename() + Constants.TEMPLATE_EXTENSION;
		String smtaGeneratedDirectory = resourceConfigBean.getGeneratedDirectory();
		String smtaGeneratedFilename = symbol + Constants.PDF_EXTENSION;
		String smtaWebsiteUrl = resourceConfigBean.getUrlSmtaWebsite();
		
		paramMap.put(Constants.KEY_SMTA_TEMPLATE_DIRECTORY, smtaTemplateDirectory);
		paramMap.put(Constants.KEY_SMTA_TEMPLATE_FILENAME, smtaTemplateFilename);
		paramMap.put(Constants.KEY_SMTA_GENERATED_DIRECTORY, smtaGeneratedDirectory);
		paramMap.put(Constants.KEY_SMTA_GENERATED_FILENAME, smtaGeneratedFilename);
		paramMap.put(Constants.KEY_IRRI_SMTA_ID, symbol);
		paramMap.put(Constants.KEY_SHU_REQUEST_ID, shuRequestId);
		paramMap.put(Constants.KEY_SMTA_WEBSITE_URL, smtaWebsiteUrl);
		
		try {
			smtaGenerateService.generatePdf(paramMap, Constants.MTA_TYPE_0);
			items.put("filename", smtaGeneratedFilename);
			
			return mapper.writeValueAsString(items);
		} catch (Exception e) {
			// Add exception to report log
			reportLogService.addReportLog(e.getMessage());
			LOGGER.error(e.getMessage());
		}
		return "";
	}


	@RequestMapping(value = "/mtagenerate/mtaType/{mtaType}/symbol/{symbol}/shuRequestId/{shuRequestId}", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public String getGenerateMtaAsJson(@PathVariable(value = "mtaType") String mtaType, @PathVariable(value = "symbol") String symbol, @PathVariable(value = "shuRequestId") String shuRequestId) {
		LOGGER.info("Generating PDF for MTA TYPE = " + mtaType + " and SMTA ID = " + symbol + " and SHU REQUEST ID = " + shuRequestId);
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> items = new HashMap<String, Object>();
		
		Map<String, String> paramMap = new HashMap<String, String>();
		String mtaTemplateDirectory = resourceConfigBean.getMtaTemplateDirectory();
		String mtaTemplateFilename = resourceConfigBean.getMtaTemplateFilename() + mtaType + Constants.TEMPLATE_EXTENSION;
		String mtaGeneratedDirectory = resourceConfigBean.getGeneratedDirectory();
		String mtaGeneratedFilename = symbol + Constants.MTA_SUFFIX + Constants.PDF_EXTENSION;
		String smtaWebsiteUrl = resourceConfigBean.getUrlSmtaWebsite();
		
		paramMap.put(Constants.KEY_MTA_TEMPLATE_DIRECTORY, mtaTemplateDirectory);
		paramMap.put(Constants.KEY_MTA_TEMPLATE_FILENAME, mtaTemplateFilename);
		paramMap.put(Constants.KEY_MTA_GENERATED_DIRECTORY, mtaGeneratedDirectory);
		paramMap.put(Constants.KEY_MTA_GENERATED_FILENAME, mtaGeneratedFilename);
		paramMap.put(Constants.KEY_IRRI_SMTA_ID, symbol);
		paramMap.put(Constants.KEY_SHU_REQUEST_ID, shuRequestId);
		paramMap.put(Constants.KEY_SMTA_WEBSITE_URL, smtaWebsiteUrl);
		
		try {
			smtaGenerateService.generatePdf(paramMap, mtaType);
			items.put("filename", mtaGeneratedFilename);
			
			return mapper.writeValueAsString(items);
		} catch (Exception e) {
			// Add exception to report log
			reportLogService.addReportLog(e.getMessage());
			LOGGER.error(e.getMessage());
		}
		return "";
	}


}
